package com.example.dllo.exe.fenlei.gonglue;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dllo.exe.R;
import com.example.dllo.exe.bean.GongLueRvBean;
import com.example.dllo.exe.fengzhuang.NetTool;

/**
 * code is far away from bug with the animal protecting
 * <p>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class GongLueRvUpAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private GongLueRvBean gongLueRvBean;
    private Itemclick itemonClick;

    public void setItemonClick(Itemclick itemonClick) {
        this.itemonClick = itemonClick;
    }

    public interface Itemclick {
        void Itemclick(int pos, String name);
    }

    public void setGongLueRvBean(GongLueRvBean gongLueRvBean) {
        this.gongLueRvBean = gongLueRvBean;
        notifyDataSetChanged();
    }

    public GongLueRvUpAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 11) {
            return 11;
        }
        return super.getItemViewType(position);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case 11:
                View view1 = LayoutInflater.from(context).inflate(R.layout.gonglueupfragmentsecondlayout, parent, false);
                holder = new TwoViewHoloder(view1);
                break;
            default:
                View view = LayoutInflater.from(context).inflate(R.layout.gonglueupfragmentlayout, parent, false);
                holder = new OneViewHolder(view);

        }

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        int viewType = getItemViewType(position);
        switch (viewType) {
            case 11:
                TwoViewHoloder twoViewHoloder = (TwoViewHoloder) holder;
                twoViewHoloder.buttonall.setText("点击查看全部");
                break;
            default:
                OneViewHolder oneViewHolder = (OneViewHolder) holder;
                NetTool netTool = new NetTool();

                netTool.getImage(gongLueRvBean.getData().getColumns().get(position).getCover_image_url(), oneViewHolder.coverimageurl);
                try {
                    oneViewHolder.titleuptitle.setText(gongLueRvBean.getData().getColumns().get(position).getTitle());
                    oneViewHolder.subtitleuptitle.setText(gongLueRvBean.getData().getColumns().get(position).getSubtitle());
                    oneViewHolder.authoruptitle.setText(gongLueRvBean.getData().getColumns().get(position).getAuthor());
                    oneViewHolder.titleuptitle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                            String name = gongLueRvBean.getData().getColumns().get(position).getTitle();
                            itemonClick.Itemclick(position, name );
                        }
                    });
                    oneViewHolder.subtitleuptitle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {


                            String name = gongLueRvBean.getData().getColumns().get(position).getTitle();
                            itemonClick.Itemclick(position, name );
                        }
                    });
                    oneViewHolder.authoruptitle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {


                            String name = gongLueRvBean.getData().getColumns().get(position).getTitle();
                            itemonClick.Itemclick(position, name );
                        }
                    });
                    oneViewHolder.coverimageurl.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {


                            String name = gongLueRvBean.getData().getColumns().get(position).getTitle();
                            itemonClick.Itemclick(position, name );
                        }
                    });
                } catch (NullPointerException e) {
                    Log.d("GongLueRvUpAdapter", "GongLueRvAdapter" + position);
                }
        }

    }

    @Override
    public int getItemCount() {
        return 12;
    }

    private class TwoViewHoloder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private Button buttonall;

        public TwoViewHoloder(View itemView) {
            super(itemView);
            buttonall = (Button) itemView.findViewById(R.id.button_all);
            buttonall.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, GongLueLanMuChaKanQuanBu.class);
            context.startActivity(intent);
        }
    }

    private class OneViewHolder extends RecyclerView.ViewHolder {
        TextView titleuptitle;
        TextView subtitleuptitle;
        TextView authoruptitle;
        ImageView coverimageurl;

        public OneViewHolder(View itemView) {
            super(itemView);
            titleuptitle = (TextView) itemView.findViewById(R.id.title_textrv);
            subtitleuptitle = (TextView) itemView.findViewById(R.id.subtitle_textrv);
            authoruptitle = (TextView) itemView.findViewById(R.id.author_textrv);
            coverimageurl = (ImageView) itemView.findViewById(R.id.coverimage_iv);
        }
    }
}
